package com.sqfw.project.community.service.impl;

import com.sqfw.project.community.domain.ReplyStatisticsBO;
import com.sqfw.project.community.domain.ServiceLoadBO;
import com.sqfw.project.community.domain.WpTraceBO;
import com.sqfw.project.community.mapper.StatisticsMapper;
import com.sqfw.project.community.service.StatisticsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class StatisticsServiceImpl implements StatisticsService {

    @Autowired
    private StatisticsMapper statisticsMapper;

    @Override
    public List<ServiceLoadBO> civilTop(WpTraceBO wpTrace) {

        return statisticsMapper.civilTop(wpTrace);
    }

    @Override
    public ReplyStatisticsBO replyStatistics(WpTraceBO wpTrace) {
        //回访数量统计（答复未答复件）
        List<Map<String, Object>> replayList = statisticsMapper.replyStatistics(wpTrace);
        ReplyStatisticsBO replyStatisticsBO = new ReplyStatisticsBO();
        if (replayList != null && replayList.size() > 0) {
            replayList.stream().forEach((item) -> {
                String isReply = (String)item.get("IS_REPLY");
                BigDecimal num = new BigDecimal(item.get("NUM").toString());
                if ("1".equals(isReply)) {
                    replyStatisticsBO.setReply(num);
                } else {
                    replyStatisticsBO.setNoReply(num);
                }

            });
            DecimalFormat df = new DecimalFormat("0.00");
            BigDecimal allReply = replyStatisticsBO.getReply();
            if (replyStatisticsBO.getNoReply() != null) {
                allReply = replyStatisticsBO.getReply().add(replyStatisticsBO.getNoReply());
            }

            replyStatisticsBO.setReplyRate(allReply.compareTo(BigDecimal.ZERO)!=0?df.format(replyStatisticsBO.getReply().doubleValue() / allReply.doubleValue() * 100.0) + "%":"0.00%");
        }

        List<Map<String, Object>> satisfieldList = statisticsMapper.satisfiedStatistics(wpTrace);

        if (satisfieldList != null && satisfieldList.size() > 0) {
            satisfieldList.stream().forEach((item) -> {
                String issatisfied = (String)item.get("ISSATISFIED");
                BigDecimal num = new BigDecimal(item.get("NUM").toString());
                if ("满意".equals(issatisfied)) {
                    replyStatisticsBO.setSatisfied(num);
                } else if ("不满意".equals(issatisfied)) {
                    replyStatisticsBO.setNoSatisfied(num);
                } else {
                    replyStatisticsBO.setCommon(num);
                }

            });
        }

        return replyStatisticsBO;
    }

    @Override
    public Map<String, Object> civilAppeal(WpTraceBO wpTrace) {
        Map<String, Object> resultMap = new HashMap<>();
        //昨日受理
        int yesterdayAcceptance = statisticsMapper.yesterdayAcceptance(wpTrace);
        //昨日提交
        int yesterdaySubmit = statisticsMapper.yesterdaySubmit(wpTrace);
        //即将超时
        int toTimeout = statisticsMapper.AboutToTimeoutCount(wpTrace);
        //超时未办结
        int timeoutNoFinish = statisticsMapper.timeoutNoFinish(wpTrace);
        resultMap.put("yesterdayAcceptance", yesterdayAcceptance);
        resultMap.put("yesterdaySubmit", yesterdaySubmit);
        resultMap.put("toTimeout", toTimeout);
        resultMap.put("timeoutNoFinish", timeoutNoFinish);
        return resultMap;
    }
}
